home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
textob
/
textobj.cls
< prev
next >
Wrap
Text File
|
1996-03-06
|
13KB
|
490 lines
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "TextFile"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit
'-- Size of buffer when reading file
' Default = 32K, Max = 64K
Public BlockSize As Long
'-- Error number
Public ErrorNum As Long
'-- Error message
Public ErrorMsg As String
'-- Contains the file data
Private szData() As String
'-- Number of lines
Private lLines As Long
'-- Holds the current line when searching
Private lCurLine As Long
'-- Holds the current position in the current
' line when searching
Private lCurPos As Long
'-- String being searched
Private szSearch As String
'-- Case of search term
Private nSearchCase As Integer
'-- Bad input file name specified
Private Const ERROR_BAD_FILENAME = 1
'-- No data to save
Private Const ERROR_NO_DATA = 2
'-- No file specified when saving
Private Const ERROR_NO_FILE_SPECIFIED = 3
'-- Could not write to file
Private Const ERROR_FILE_WRITE = 4
'-- Error creating new data
Private Const ERROR_CREATE_NEW = 5
Public Sub CreateNew(lLineCount As Long)
Me.ErrorNum = False
Me.ErrorMsg = ""
On Error Resume Next
ReDim szData(1 To lLineCount) As String
If Err Then
Me.ErrorNum = vbObjectError + ERROR_CREATE_NEW
Me.ErrorMsg = Error
Exit Sub
End If
lLines = lLineCount
End Sub
Public Property Let FoundPos(ByVal nDummy As Integer)
'-- Do not let the user set the search position.
End Property
Public Property Get FoundPos() As Integer
'-- Return the current position within the current line of
' the searched for and found text.
FoundPos = lCurPos
End Property
Public Property Get Line(ByVal lIndex As Long) As String
'-- Retreives a line of text from the file.
'-- Trap errors
On Error Resume Next
Line = szData(lIndex)
End Property
Public Property Let Line(ByVal lIndex As Long, ByVal szText As String)
'-- Trim Cr and LF chars
If Right(szText, 1) = vbLf Then
szText = Left$(szText, Len(szText) - 1)
End If
If Right(szText, 1) = vbCr Then
szText = Left$(szText, Len(szText) - 1)
End If
'-- Return the array element (no CR/LF)
szData(lIndex) = szText
End Property
Public Property Get Lines() As Long
'-- Number of lines
Lines = lLines
End Property
Public Property Let Lines(ByVal lDummy As Long)
'-- Do not allow the user to set the number of lines.
End Property
Public Sub Load(ByVal szFileName As String)
'-- Load the contents of a text file into memory
' This routine will handle any line that ends with
' a carriage return, a linefeed, or both.
Dim szBuffer As String
Dim lFileNum As Long
Dim lFileLen As Long
Dim lNumBlocks As Long
Dim lRemainder As Long
Dim lIndex As Long
Dim lPos As Long
Me.ErrorNum = False
Me.ErrorMsg = ""
'-- Open the file
lFileNum = FreeFile
Open szFileName For Binary As lFileNum
lFileLen = LOF(lFileNum)
'-- Does the file exist?
If lFileLen = 0 Then
Close lFileNum
Me.ErrorNum = vbObjectError + ERROR_BAD_FILENAME
Me.ErrorMsg = "File Does Not Exist"
Exit Sub
End If
'-- Clear the current array
Erase szData
lLines = 0
'-- Get the buffer size
If BlockSize = 0 Then
BlockSize = 32768
ElseIf BlockSize > 65535 Then
BlockSize = 65535
End If
'-- Get the number of blocks
lNumBlocks = lFileLen \ BlockSize
'-- Anything left over?
lRemainder = lFileLen Mod BlockSize
'-- Read and process each block
For lIndex = 1 To lNumBlocks
szBuffer = Space$(BlockSize)
Get #lFileNum, , szBuffer
GoSub ProcessData
Next
'-- Process whatever's left
If lRemainder Then
szBuffer = Space$(lRemainder)
Get #lFileNum, , szBuffer
GoSub ProcessData
End If
'-- Close the file and exit
Close lFileNum
Exit Sub
ProcessData:
Do
'-- Find the next CR
lPos = InStr(szBuffer, vbCr)
If lPos Then
'-- Copy the text up to the CRLF into szData
lLines = lLines + 1
ReDim Preserve szData(1 To lLines) As String
szData(lLines) = Left$(szBuffer, lPos - 1)
'-- If the next character is a linefeed, skip over it.
If Asc(Mid$(szBuffer, lPos + 1, 1)) = 10 Then
szBuffer = Mid$(szBuffer, lPos + 2)
Else
szBuffer = Mid$(szBuffer, lPos + 1)
End If
Else
'-- Find the next LF
lPos = InStr(szBuffer, vbLf)
If lPos Then
'-- Copy the text up to the CRLF into szData
lLines = lLines + 1
ReDim Preserve szData(1 To lLines) As String
szData(lLines) = Left$(szBuffer, lPos - 1)
szBuffer = Mid$(szBuffer, lPos + 1)
Else
'-- No more CRLFs. More data?
If Len(szBuffer) Then
'-- Yep. Add it to szData
lLines = lLines + 1
ReDim Preserve szData(1 To lLines) As String
szData(lLines) = szBuffer
End If
'-- No more data. Exit the loop
Exit Do
End If
End If
Loop
Return
End Sub
Public Sub LoadListBox(ByVal szFileName As String, List1 As Control)
'-- Loads the contents of a text file into a list box
' This routine will handle any line that ends with
' a carriage return, a linefeed, or both.
Dim szBuffer As String
Dim lFileNum As Long
Dim lFileLen As Long
Dim lNumBlocks As Long
Dim lRemainder As Long
Dim lIndex As Long
Dim lPos As Long
Me.ErrorNum = False
Me.ErrorMsg = ""
'-- Open the file
lFileNum = FreeFile
Open szFileName For Binary As lFileNum
lFileLen = LOF(lFileNum)
'-- Does the file exist?
If lFileLen = 0 Then
Close lFileNum
Me.ErrorNum = vbObjectError + ERROR_BAD_FILENAME
Me.ErrorMsg = "File Does Not Exist"
Exit Sub
End If
'-- Clear the List Box
List1.Clear
'-- Get the buffer size
If BlockSize = 0 Then
BlockSize = 32768
ElseIf BlockSize > 65535 Then
BlockSize = 65535
End If
'-- Get the number of blocks
lNumBlocks = lFileLen \ BlockSize
'-- Anything left over?
lRemainder = lFileLen Mod BlockSize
'-- Read and process each block
For lIndex = 1 To lNumBlocks
szBuffer = Space$(BlockSize)
Get #lFileNum, , szBuffer
GoSub ProcessData
Next
'-- Process whatever's left
If lRemainder Then
szBuffer = Space$(lRemainder)
Get #lFileNum, , szBuffer
GoSub ProcessData
End If
'-- Close the file and exit
Close lFileNum
Exit Sub
ProcessData:
Do
'-- Find the next CR
lPos = InStr(szBuffer, vbCr)
If lPos Then
'-- Copy the text up to the CRLF into List1
List1.AddItem Left$(szBuffer, lPos - 1)
'-- If the next character is a linefeed, skip over it.
If Asc(Mid$(szBuffer, lPos + 1, 1)) = 10 Then
szBuffer = Mid$(szBuffer, lPos + 2)
Else
szBuffer = Mid$(szBuffer, lPos + 1)
End If
Else
'-- Find the next LF
lPos = InStr(szBuffer, vbLf)
If lPos Then
'-- Copy the text up to the CRLF into List1
List1.AddItem Left$(szBuffer, lPos - 1)
szBuffer = Mid$(szBuffer, lPos + 1)
Else
'-- No mo